<?php
defined('APP_PATH') or die ('Unauthorized access');
/**
 * 载入核心类
 * @author windpurple
 * @version 1.0
 */
final class Load
{
	private function __construct()
	{
		
	}
	
	/**
	 * 载入模型
	 * @param unknown_type $model
	 */
	public static function Model($model)
	{
		$models = explode('/',$model);
		foreach($models as $k => $v){
			$models[$k] = ucfirst($v);
		}
		$model = implode('/',$models);
		if(count($models) ==2){
			$model_file = PRO_PATH.'/'.$model.'.php';
			$model_name = $models[0].'_Model_'.$models[1];
		}else{
			$model_file = PRO_PATH.'/Model/'.$model.'.php';
			$model_name = 'Model_'.$model;
		}
		
		if(!file_exists($model_file)){
			Error::message('模型文件 '.ucwords($model).'.php 不存在');
		}
		
		include_once(LIB_PATH.'/Model.php');
		include_once($model_file);
		
		if(!class_exists($model_name)){
			
			Error::message('类 '.$model_name.' 未定义');
		}
		
		$model = new $model_name();
		return $model;
		
	}
	
	/**
	 * 载入视图
	 * @param unknown_type $file
	 */
	public static function View($file)
	{
		global $view;
		$view->display($file,false);
		
	}
	
	/**
	 * 载入扩展
	 * @param unknown_type $path
	 * @return object
	 * 扩展类命名规则：Ext_Classname
	 */
	public static function Ext($path)
	{
		$exts = explode('/',$path);
		foreach($exts as $k => $v){
			$exts[$k] = ucfirst($v);
		}
		$ext_class = count($exts) >= 2 ? 'Ext_'.$exts[count($exts) -1] : 'Ext_'.$exts[0];
		$exts = implode('/',$exts);
		$ext_file = LIB_PATH.'/Ext/'.$exts.'.php';
		
		if(!file_exists($ext_file)){
			Error::message('扩展 ' .$exts.'.php 不存在');
		}
		
		include_once($ext_file);
		
		if(!class_exists($ext_class)){
			Error::message('类 '.$ext_class.' 未定义');
		}
		
		$ext = new $ext_class();
		
		return $ext;

	}
	
	/**
	 * 载入缓存
	 */
	public static function Cache()
	{
		$cache_lib = LIB_PATH.'/Cache.php';
		
		if(!file_exists($cache_lib)){
			Error::message('核心文件 Cache.php 不存在');
		}
		
		include_once ($cache_lib);
		
		if(!class_exists('Cache')){
			Error::message('核心类 Cache 未定义');
		}
		
		$cache = Cache::getInstance();
		
		return $cache;
	}
	
	/**
	 * 载入控制器
	 */
	public static function Action($params)
	{
		if(!empty($params) && is_array($params)){
			$url = '';
			foreach($params as $k => $v){
				$url.= $k.'='.$v.'&';
			}
			$url = substr($url,0,-1);
			$url = 'http://'.$_SERVER['SERVER_NAME'].'/?'.$url;
			echo file_get_contents($url);
			
		}else{
			$controllers = explode('/',$params);
			foreach($controllers as $k => $v){
				$controllers[$k] = ucfirst($v);
			}
			$controller = implode('/',$controllers);
			if(count($controllers) ==2){
				$controller_file = PRO_PATH.'/'.$controller.'.php';
				$controller_name = $controllers[0].'_'.$controllers[1].'Controller';
			}else{
				$controller_file = PRO_PATH.'/Controller/'.$controller.'Controller.php';
				$controller_name = $controller.'Controller';
			}
			
			if(!file_exists($controller_file)){
				Error::message('模型文件 '.ucwords($controller).'Controller.php 不存在');
			}
			
			include_once($controller_file);
			
			if(!class_exists($controller_name)){
				
				Error::message('类 '.$controller_name.' 未定义');
			}
			
			$controller = new $controller_name();
			return $controller;
		}		
	}

}


?>